﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AWLoadTester
{
    /// <summary>
    /// Interaction logic for BrowserWindow.xaml
    /// </summary>
    public partial class BrowserWindow : Window
    {
        public string Location { get; set; }
        public Uri CurrentUri { get; set; }

        private bool Timing { get; set; }

        private System.Diagnostics.Stopwatch CurrentStopWatch { get; set; }

        private int LoadCount = 0;

        public BrowserWindow()
        {
            InitializeComponent();

            CurrentStopWatch = new System.Diagnostics.Stopwatch();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                CurrentUri = new Uri(Location);

                MainWebBrowser.Navigating += new System.Windows.Navigation.NavigatingCancelEventHandler(MainWebBrowser_Navigating);
                MainWebBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(MainWebBrowser_LoadCompleted);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error with location : [{0}] : {1}", Location, ex.Message), "Error loading web site", 
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ShowLoadString()
        {
            LocationLabel.Content = String.Format("[{0}], Loaded in {1}", Location, CurrentStopWatch.Elapsed);
        }

        void MainWebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            CurrentStopWatch.Stop();
            ShowLoadString();
        }

        void MainWebBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            CurrentStopWatch.Restart();
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LocationLabel.Content = "Loading...";
                LoadButton.IsEnabled = false;

                MainWebBrowser.Navigate(CurrentUri);

                //if (LoadCount++==0)
                //{
                //    MainWebBrowser.Navigate(CurrentUri);
                //}
                //else
                //{
                //    CurrentStopWatch.Restart();
                //    MainWebBrowser.Refresh(true);
                //    CurrentStopWatch.Stop();
                //    ShowLoadString();
                //}

                //Timing = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error with location : [{0}] : {1}", Location, ex.Message), "Error loading web site",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                Timing = false;
                LoadButton.IsEnabled = true;
                LoadButton.Content = "Load";
            }
        }
    }
}
